package com.witframework.web.tag.portal;

import java.util.Vector;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
/**
 * generates a "standard" view for portlet. Parameters are:

1) body Optional parameter. Describes your jsp file for portlet
2) column Optional parameter. Describes HTML settings for table's column with portlet.
3) border Optional parameter. Describes border. Default value is 0 (no border)
4) borderColor Optional parameter: color for the border. Default value is #0000FF (blue)
5) bodyColor Optional parameter: color for the background. Default value is #FFFFFF (white)
6) title Optional parameter. Describes title. There is no title by default
7) titleColor Optional parameter. Describes title's background. Default value is #00FF00 (green)
8) titleHeight Optional parameter. Describes title's height. Default value is 25
9) maximize Optional parameter. Describes an URL for maximize button
10) target Optional parameter. Describes target frame to hyperlink for maximize button 
 * @author wuxinyang
 * Aug 17, 2009 9:46:22 AM
 *
 */
public class Portal extends BodyTagSupport{
  private PageContext pageContext;
  private Tag parent;
  private String table = null;
  private Vector portlets = null;

  public void setTable(String paramString){
    this.table = paramString;
  }

  public String getTable()  {
    return this.table;
  }

  public void doInitBody()    throws JspException  {
    this.portlets = new Vector();
  }

  public int doEndTag()    throws JspException  {
    int i = (!(this.pageContext.getOut().isAutoFlush())) ? 1 : 0;
    try    {
      this.pageContext.getOut().write("<table");
      if (this.table != null)
        this.pageContext.getOut().write(" " + this.table);
      this.pageContext.getOut().write("><tr>");
      for (int j = 0; j < this.portlets.size(); ++j)      {
        String str;
        PortletBean localportletBean = (PortletBean)this.portlets.elementAt(j);
        this.pageContext.getOut().write("<td");
        if ((str = localportletBean.getColumn()) != null)
          this.pageContext.getOut().write(" " + str);
        this.pageContext.getOut().write(" valign='top'>");
        if (i != 0)
          this.pageContext.getOut().flush();
        writePortlet(localportletBean);
        if (i != 0)
          this.pageContext.getOut().flush();
        this.pageContext.getOut().write("</td>");
      }
      this.pageContext.getOut().write("</tr></table>");
    }
    catch (Exception localException)    {
      throw new JspException("IO Error: " + localException.getMessage());
    }
    dropData();
    return 6;
  }

  public void release()  {
    dropData();
  }

  private void dropData()  {
    this.table = null;
    this.portlets = null;
  }

  public void setPageContext(PageContext paramPageContext)  {
    this.pageContext = paramPageContext;
  }

  public void setParent(Tag paramTag)  {
    this.parent = paramTag;
  }

  public Tag getParent()  {
    return this.parent;
  }

  public void addPortlet(PortletBean paramportletBean)  {
    this.portlets.addElement(paramportletBean);
  }

  private void writePortlet(PortletBean paramportletBean)    throws JspException  {
    String str1 = paramportletBean.getBorder();
    String str2 = paramportletBean.getBodyFile();
    String str3 = paramportletBean.getBodyData();
    StringBuffer localStringBuffer = new StringBuffer("<table");
    if (!("0".equals(str1)))
      localStringBuffer.append(" cellpadding=\"" + str1 + "\"");
    else
      localStringBuffer.append(" cellpadding=\"0\"");
    localStringBuffer.append(" cellspacing=\"0\"><tr>\n");
    localStringBuffer.append("<td  ");
    if (!("0".equals(str1)))    {
      localStringBuffer.append(" bgcolor=\"");
      localStringBuffer.append(paramportletBean.getBorderColor());
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" align=\"center\">\n");
    localStringBuffer.append("<table width=\"100%\" cellpadding=\"0\" border=\"0\">\n");
    if (paramportletBean.getTitle() != null)
      if (paramportletBean.getMaximize() != null)
      {
        localStringBuffer.append("<tr><td  bgcolor=\"");
        localStringBuffer.append(paramportletBean.getTitleColor());
        localStringBuffer.append("\" height=\"");
        localStringBuffer.append(paramportletBean.getTitleHeight());
        localStringBuffer.append("\">");
        localStringBuffer.append("<table border=0 cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr bgcolor=\"");
        localStringBuffer.append(paramportletBean.getTitleColor());
        localStringBuffer.append("\"><td>");
        localStringBuffer.append(paramportletBean.getTitle());
        localStringBuffer.append("</td><td align=right>");
        localStringBuffer.append("<a href=\"");
        localStringBuffer.append(paramportletBean.getMaximize());
        localStringBuffer.append("\"");
        if (paramportletBean.getTarget() != null)
          localStringBuffer.append(" target=\"" + paramportletBean.getTarget() + "\"");
        localStringBuffer.append(">[]</a></td></tr></table></td></tr>\n");
      }
      else
      {
        localStringBuffer.append("<tr><td bgcolor=\"");
        localStringBuffer.append(paramportletBean.getTitleColor());
        localStringBuffer.append("\" height=\"");
        localStringBuffer.append(paramportletBean.getTitleHeight());
        localStringBuffer.append("\">");
        localStringBuffer.append(paramportletBean.getTitle());
        localStringBuffer.append("</td></tr>\n");
      }
    localStringBuffer.append("<tr><td bgcolor=\"");
    localStringBuffer.append(paramportletBean.getBodyColor());
    localStringBuffer.append("\">\n");
    JspWriter localJspWriter = this.pageContext.getOut();
    try
    {
      localJspWriter.print(localStringBuffer.toString());
      if (str3 != null)
      {
        localJspWriter.print(str3);
      }
      else
      {
        this.pageContext.getOut().flush();
        if (str2 != null)
          this.pageContext.include(str2);
        this.pageContext.getOut().flush();
      }
    }
    catch (Exception localException1)
    {
      throw new JspException("Could not wrap Portlet");
    }
    try
    {
      localJspWriter.println("</td></tr></table>");
      localJspWriter.println("</td></tr></table>");
    }
    catch (Exception localException2)
    {
      throw new JspException("Could not wrap Portlet");
    }
  }
}